#!/bin/bash

source ./env.sh

function compose () {
    if ! command -v docker-compose > /dev/null 2>&1 ;then
        echo "[INFO] Install DockerCompose."
        bash <(curl -sSL https://gitee.com/yx571304/olz/raw/master/shell/docker/install.sh) -i compose
    fi

    echo "[INFO] Start docker-compose.yml."
    mkdir -p /swarm/volumes/elasticsearch{1,2,3}
    chown -Rf 1000:1000 /swarm/volumes/elasticsearch{1,2,3}
    docker-compose up -d
}

function swarm () {
    echo "[INFO] Start es-kibana.yml."
    mkdir -p /swarm/volumes/elasticsearch
    chown -Rf 1000:1000 /swarm/volumes/elasticsearch
    docker stack deploy -c es-kibana.yml es-kibana
}


function log_pilot () {
    echo "[INFO] Get Local IP."
    HOST_IF=$(ip route|grep default|cut -d ' ' -f5)
    HOST_IP=$(ip a|grep "$HOST_IF$"|awk '{print $2}'|cut -d'/' -f1)


    echo """

    [INFO] Start log-pilot.
    ---------------------------------------------------------------------

    docker run -itd --restart=always \\
      --name log-pilot \\
      -v /:/host:ro \\
      -v /etc/localtime:/etc/localtime \\
      -v /var/run/docker.sock:/var/run/docker.sock \\
      --cap-add=SYS_ADMIN \\
      -e LOGGING_OUTPUT=elasticsearch \\
      -e ELASTICSEARCH_HOSTS=172.18.0.1:9200 \\
    harbor.hc-yun.com/monitor/log-pilot:v0.97
    """
}

function usage () {
    echo -e "\n\tUsage: $0 {docker-compose|swarm}"
}

case $1 in
    docker-compose)
        compose
        log_pilot
        ;;
    swarm)
        swarm
        log_pilot
        ;;
    *)
        usage ;;
esac
